<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="单位名称" prop="companyId">
      <el-input v-model="dataForm.companyId" placeholder="单位名称"></el-input>
    </el-form-item>
    <el-form-item label="内设机构" prop="installation">
      <el-input v-model="dataForm.installation" placeholder="内设机构"></el-input>
    </el-form-item>
    <el-form-item label="产品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="产品名称"></el-input>
    </el-form-item>
    <el-form-item label="品牌" prop="brand">
      <el-input v-model="dataForm.brand" placeholder="品牌"></el-input>
    </el-form-item>
    <el-form-item label="型号" prop="model">
      <el-input v-model="dataForm.model" placeholder="型号"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="number">
      <el-input v-model="dataForm.number" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="产品类型" prop="productType">
      <el-input v-model="dataForm.productType" placeholder="产品类型"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          companyId: '',
          installation: '',
          productName: '',
          brand: '',
          model: '',
          number: '',
          productType: '',
          remarks: ''
        },
        dataRule: {
          companyId: [
            { required: true, message: '单位名称不能为空', trigger: 'blur' }
          ],
          installation: [
            { required: true, message: '内设机构不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          brand: [
            { required: true, message: '品牌不能为空', trigger: 'blur' }
          ],
          model: [
            { required: true, message: '型号不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          productType: [
            { required: true, message: '产品类型不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/auterminal/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.companyId = data.auterminal.companyId
                this.dataForm.installation = data.auterminal.installation
                this.dataForm.productName = data.auterminal.productName
                this.dataForm.brand = data.auterminal.brand
                this.dataForm.model = data.auterminal.model
                this.dataForm.number = data.auterminal.number
                this.dataForm.productType = data.auterminal.productType
                this.dataForm.remarks = data.auterminal.remarks
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/auterminal/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'companyId': this.dataForm.companyId,
                'installation': this.dataForm.installation,
                'productName': this.dataForm.productName,
                'brand': this.dataForm.brand,
                'model': this.dataForm.model,
                'number': this.dataForm.number,
                'productType': this.dataForm.productType,
                'remarks': this.dataForm.remarks
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
